package com.vcredit.creditcat.manager.product;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.api.IShanDaiManager;
import com.vcredit.creditcat.dto.product.CreditCRFAmountParamDto;
import com.vcredit.creditcat.enumtype.MerchantProductEnum;
import com.vcredit.creditcat.enumtype.SmsDescEnum;
import com.vcredit.creditcat.manager.SMSMessageManager;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.DateUtil;
import com.vcredit.framework.util.JacksonUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class SDProductManager implements IShanDaiManager {



	@Autowired
	private CustProductApplyService custProductApplyService;
	@Autowired
	private CustService custService;
	@Autowired
	private ProductApplyManager productApplyManager;
	@Autowired
	private CustAccountService custAccountService;
	@Autowired
	public SMSMessageManager sMSMessageManager;

	@Log
	@Override
	public void dealResult(String productName, String jsonParam) throws Exception {		
		productApplyManager.noticeApplyResult(this.handleResult(jsonParam.toString()), productName);
	}

	
	@Log
	public CustProductApply handleResult(String jsonEntry) throws Exception {
		CreditCRFAmountParamDto result = JacksonUtil.Json2Obj(jsonEntry, new TypeReference<CreditCRFAmountParamDto>() {
		});
		String idNo = result.getIdNo();
		Cust cust = custService.findByIdNo(idNo);
			
		CustProductApply custProductApply = new CustProductApply();
		custProductApply.setCustId(cust.getCustId());
		custProductApply.setProductId(new Long(MerchantProductEnum.creditDdh.getCode()));
		List<CustProductApply> list = custProductApplyService.findByConditions(custProductApply);
		if (list.size() > 0) {
			custProductApply.setApplyId(list.get(0).getApplyId());
		}
		String flowNo = Utility.nextCode(MerchantProductEnum.creditDdh.name().toUpperCase());
		custProductApply.setFlowNo(flowNo);
	
		// 处理授信状态
		String creditStatus = result.getOrderState();
		if(creditStatus==null){
			throw new Exception("闪贷回调返回状态异常");
		}
		switch (creditStatus) {
		case "7": // 已完成授信
			custProductApply.setAuditResult(1);
			custProductApply.setIsApply(false);
			custProductApply.setCreditAmt(result.getCreditAmt() == null ? 0 : result.getCreditAmt());
			if(StringUtils.isNotEmpty(result.getCreditExpiretime())){
				long time = Long.parseLong(result.getCreditExpiretime());
				SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				String creditExpiredTime = format.format(time);
				custProductApply.setCreditExpiretime(creditExpiredTime);
			}		
			custProductApply.setServiceRate(result.getServiceRate());
			custProductApply.setTotalServiceRate(result.getTotalServiceRate());
			custProductApply.setFormalitiesRate(result.getFormalitiesRate());
			custProductApply.setInterestRateMonthly(result.getInterestRateMonthly());
			custProductApply.setRejectMsg("授信成功");
			
			try { //发送成功短信，不影响主流程
				CustAccount account = new CustAccount();
				account.setCustId(cust.getCustId());
				CustAccount custAccount = custAccountService.find(account).get(0);
				String mobile = custAccount.getUserName();
				String appdesc=custAccount.getAppDesc();	
				String userName = custAccount.getAccountRealname();
				appdesc=appdesc.split(";")[0].toString();
				String name=SmsDescEnum.getNameByAppDesc(appdesc);
				sMSMessageManager.sendCreditSuccess(mobile, name, userName);
			} catch (Exception e) {
				
			}
						
			return custProductApply;
		case "8": // 拒绝授信
			custProductApply.setAuditResult(0);
			if(result.getRemark()!=null){
				custProductApply.setRejectMsg("闪贷授信拒绝："+result.getRemark());
			}else{
				custProductApply.setRejectMsg("闪贷授信拒绝");
			}			
			custProductApply.setIsApply(true);
			custProductApply.setCreditApplyTime(DateUtil.formatDateTime(DateUtil.addDayTime(new Date(),30)));
			
			try {  //发送失败短信，不影响主流程
				CustAccount account = new CustAccount();
				account.setCustId(cust.getCustId());
				CustAccount custAccount = custAccountService.find(account).get(0);
				String mobile = custAccount.getUserName();
				String appdesc = custAccount.getAppDesc();	
				String userName = custAccount.getAccountRealname();
				appdesc=appdesc.split(";")[0].toString();
				String name=SmsDescEnum.getNameByAppDesc(appdesc);
				sMSMessageManager.sendCreditFail(mobile, name, userName);
			} catch (Exception e) {
				
			}
			return custProductApply;			
			
		case "9": // 信用补救，可重新授信
			custProductApply.setAuditResult(7);
			custProductApply.setRejectMsg("闪贷信用补救");
			custProductApply.setIsApply(false);
			return custProductApply;
		default: 
			return custProductApply;
		}
	}
		
}
